//
//  VisitorView.swift
//  weibo
//
//  Created by HJ on 16/3/13.
//  Copyright © 2016年 HJ. All rights reserved.
//

import UIKit
import SnapKit

protocol visitorViewDelegate:NSObjectProtocol {
    func userWillLogin()
    func userWillSignUp()
}

class VisitorView: UIView {
    
    weak var visitorDelegate:visitorViewDelegate?
    

    func setupInfo(imgName: String?, tipLabelStr: String) {
        tipLabel.text = tipLabelStr
        if imgName != nil {
            bigWheel.image = UIImage(named: imgName!)
            bigHouse.hidden = true
            maskImgView.hidden = true
            
        }else {
            startAnimation()

        }
   }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(red: 236.0 / 255.0, green: 236.0 / 255.0, blue: 236.0 / 255.0, alpha: 1)
        addSubview(bigHouse)
        addSubview(bigWheel)
        addSubview(tipLabel)
        addSubview(maskImgView)
        bringSubviewToFront(bigHouse)
        addSubview(loginBtn)
        addSubview(signUpBtn)
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        signUpBtn.addTarget(self, action: "signUpBtnClick", forControlEvents: .TouchUpInside)
    }
    
   @objc private func loginBtnClick() {
        visitorDelegate?.userWillLogin()
    }
    
    @objc private func signUpBtnClick() {
        visitorDelegate?.userWillSignUp()
    }
    
    
    private lazy var bigHouse: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "visitordiscover_feed_image_house")
        imgView.sizeToFit()
        return imgView
    }()
    
    private lazy var bigWheel:UIImageView = {
        let wheelView = UIImageView()
        wheelView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        wheelView.sizeToFit()
        return wheelView
    }()
    
    private lazy var tipLabel:UILabel = {
        let label = UILabel()
//        label.text = "关注一些人,回到这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(15)
        label.sizeToFit()
        return label
    }()
    
    private lazy var maskImgView:UIImageView = {
        let mask  = UIImageView()
        mask.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        mask.sizeToFit()
        
        return mask
    }()
    
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        let backImg = UIImage(named: "common_button_white_disable")
        let capW = Int(backImg!.size.width * 0.5)
        let capH = Int(backImg!.size.height * 0.5)

        btn.setBackgroundImage(backImg?.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        btn.setTitle("登陆", forState: .Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        return btn
        
    }()
    
    private lazy var signUpBtn: UIButton = {
        let btn = UIButton()
        let backImg = UIImage(named: "common_button_white_disable")
        let capW = Int(backImg!.size.width * 0.5)
        let capH = Int(backImg!.size.height * 0.5)
        
        btn.setBackgroundImage(backImg?.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        return btn
        
    }()
    
    
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        bigWheel.layer.addAnimation(anim, forKey: nil)
        
    }
    
    // 添加约束
    override func layoutSubviews() {
        bigHouse.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        bigWheel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(bigHouse.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(bigWheel.snp_bottom).offset(20)
            make.centerX.equalTo(self.snp_centerX)
            make.height.equalTo(40)
            make.width.equalTo(250)

        }
        
        maskImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(bigWheel.snp_center)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            make.width.equalTo(100)
        }
        
        signUpBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            make.width.equalTo(100)
        }
    }
    
    

}
